package com.leruan.aasimp.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.CarInfoDao;
import com.leruan.aasimp.entity.CarInfo;

public class CarInfoDaoImpl extends HibernateDaoSupport implements CarInfoDao {

	public void saveMtInfo(CarInfo carInfo) {
		Session session = this.getSession();
		session.save(carInfo);
	}

	@SuppressWarnings("unchecked")
	public List<CarInfo> getAllMtInfo(int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInfo where infotype='1' and status='1' order by releaseTime desc");
		Query query = session.createQuery(sb.toString());
		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);
		List<CarInfo> list = query.list();
		return list;
	}

	public Long getMtCount() {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select count(id) from CarInfo where infotype='1' and status='1'");
		return (Long)session.createQuery(sb.toString()).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<CarInfo> getAllPtInfo(int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInfo where infotype='2' and status='1' order by releaseTime desc");
		Query query = session.createQuery(sb.toString());
		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);
		List<CarInfo> list = query.list();
		return list;
	}

	public Long getPtCount() {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select count(id) from CarInfo where infotype='2' and status='1'");
		return (Long)session.createQuery(sb.toString()).uniqueResult();
	}

	public CarInfo getMtById(String id) {
		Long idd = Long.parseLong(id);
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInfo where id=?");
		return (CarInfo) session.createQuery(sb.toString()).setLong(0, idd).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<CarInfo> getAllMrInfo(int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInfo where infotype='3' and status='1' order by releaseTime desc");
		Query query = session.createQuery(sb.toString());
		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);
		List<CarInfo> list = query.list();
		return list;
	}

	public Long getMrCount() {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select count(id) from CarInfo where infotype='3' and status='1'");
		return (Long)session.createQuery(sb.toString()).uniqueResult();
	}

}
